package com.zecan.blog.security.entity;

import com.zecan.blog.entity.BlogUser;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.Assert;

import java.util.Collection;

/**
 * @ClassName AccountUser
 * @Description
 * @date 2022/10/29 20:08
 * @Version 1.0
 * 当前类是用户进行认证完毕之后,存放在SecurityContextHolder中的用户数据信息
 */
@Data
@NoArgsConstructor
public class AccountUser implements UserDetails {

    private Long userId; //用户id

    private Boolean rememberMe; // 记住我

    private String captchaCode ; // 验证码

    private String captchaKey; // 验证码标识符

    private String nickname; // 用户昵称

    private String email; //用户邮箱

    private String userAvatar; // 用户头像

    private Integer userArticleCount;

    private static final long serialVersionUID = 540L;
    private static final Log logger = LogFactory.getLog(BlogUser.class);
    private String password; // 封装的密码
    private  String username; // 封装的用户名
    private  Collection<? extends GrantedAuthority> authorities; //权限
    private  boolean accountNonExpired; // 当前是否过期
    private  boolean accountNonLocked; // 当前是否锁定
    private  boolean credentialsNonExpired; //认证是否过期
    private  boolean enabled; //是否可以用

    public AccountUser(String username, String password , String captchaCode, String captchaKey, Boolean rememberMe) {
        this.username = username;
        this.password = password;
        this.captchaCode =  captchaCode;
        this.captchaKey = captchaKey;
        this.rememberMe = rememberMe;
    }

    public AccountUser(Long userId, String username,
                       String password, Collection<? extends GrantedAuthority> authorities,
                       String userAvatar,String nickname,String email,Integer userArticleCount) {
        this(userId, username, password, true, true, true, true,authorities,userAvatar,nickname,email,userArticleCount);
    }

    public AccountUser(Long userId, String username, String password,
                       boolean enabled, boolean accountNonExpired,
                       boolean credentialsNonExpired, boolean accountNonLocked
            , Collection<? extends GrantedAuthority> authorities,String userAvatar
            , String nickname, String email,Integer userArticleCount) {
        // 断言 只有在后面的语句是真的情况才能往下执行, 否则报错
        Assert.isTrue(username != null && !"".equals(username) && password != null, "Cannot pass null or empty values to constructor");
        this.userId = userId;
        this.username = username;
        this.password = password;
        this.enabled = enabled;
        this.accountNonExpired = accountNonExpired;
        this.credentialsNonExpired = credentialsNonExpired;
        this.accountNonLocked = accountNonLocked;
        this.authorities = authorities;
        this.userAvatar = userAvatar;
        this.nickname = nickname;
        this.email = email;
        this.userArticleCount = userArticleCount;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return this.accountNonExpired;
    }

    @Override
    public boolean isAccountNonLocked() {
        return this.accountNonLocked;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return this.credentialsNonExpired;
    }

    @Override
    public boolean isEnabled() {
        return this.enabled;
    }
}


